﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceGame.Gameplay
{
    public class LocalShip : Ship
    {
        
        /// <summary>
        /// Identifies ship.
        /// </summary>
        private PlayerIndex playerIndex;

        /// <summary>
        /// Initializes local ship.
        /// </summary>
        /// <param name="world"></param>
        /// <param name="playerIndex"></param>
        /// <param name="color"></param>
        public LocalShip(World world, PlayerIndex playerIndex, Color color) : base(world, color)
        {
            this.playerIndex = playerIndex;
        }


        /// <summary>
        /// Process local input and change input state.
        /// </summary>
        /// <param name="elapsedTime"></param>
        public override void CaptureInput(float elapsedTime)
        {
            //captures input

            GamePadState gamePadState = GamePad.GetState(playerIndex);
            KeyboardState keyboardState = Keyboard.GetState(playerIndex);

			ScreenInput.ScreenPadState screenState = GameplayScreen.screenPad.GetState();

            if(keyboardState.IsKeyDown(Keys.A))
            {
                input.TriedJoined = true;
            } 
            else if (keyboardState.IsKeyDown(Keys.B))
            {
                input.TriedLeaving = true;
            }

			if (screenState.Buttons.A == ScreenInput.ButtonState.Pressed)
			{
				input.TriedJoined = true;
			}
			else if (screenState.Buttons.B == ScreenInput.ButtonState.Pressed)
			{
				input.TriedLeaving = true;
			}


            // check for movement action
            bool keyHit = false;

            input.LeftStick = Vector2.Zero;

            if (keyboardState.IsKeyDown(Keys.W))
            {
                input.LeftStick += Vector2.UnitY;
                keyHit = true;
            }
            if (keyboardState.IsKeyDown(Keys.A))
            {
                input.LeftStick += Vector2.Multiply(Vector2.UnitX, -1f);
                keyHit = true;
            }
            if (keyboardState.IsKeyDown(Keys.S))
            {
                input.LeftStick += Vector2.Multiply(Vector2.UnitY, -1f);
                keyHit = true;
            }
            if (keyboardState.IsKeyDown(Keys.D))
            {
                input.LeftStick += Vector2.UnitX;
                keyHit = true;
            }
            if (keyHit)
            {
                //if (LeftStick.LengthSquared() > 0)
                //{
                //   LeftStick.Normalize();
                //}
            }
            else
            {
				input.LeftStick = screenState.ThumbSticks.Left;  //gamePadState.ThumbSticks.Left;
            }

            input.LeftStick.Y *= -1f;


            // check for firing action
            keyHit = false;
            input.RightStick = Vector2.Zero;
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                input.RightStick += Vector2.UnitY;
                keyHit = true;
            }
            if (keyboardState.IsKeyDown(Keys.Left))
            {
                input.RightStick += Vector2.Multiply(Vector2.UnitX, -1f);
                keyHit = true;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                input.RightStick += Vector2.Multiply(Vector2.UnitY, -1f);
                keyHit = true;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                input.RightStick += Vector2.UnitX;
                keyHit = true;
            }
            if (keyHit)
            {
                //if (RightStick.LengthSquared() > 0)
                //{
                //   RightStick.Normalize();
                //}
            }
            else
            {
				input.RightStick = screenState.ThumbSticks.Right;     //gamePadState.ThumbSticks.Right;
            }

            input.RightStick.Y *= -1f;

            // check for firing a mine
            keyHit = false;
            input.MineFired = false;
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                input.MineFired = true;
                keyHit = true;
            }
            if (!keyHit)
            {
                input.MineFired = gamePadState.Triggers.Right >= 0.9f;
            }
        }

    }
}
